// pages/ucenter/collection/collection.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //saled为库存
        collectionList: [
            { 
              id:'',
              name: '追源杨梅干', 
              img: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-b1ebbd3c-ca49-405b-957b-effe60782276/c8c4e05e-42d4-4a22-b111-85765d4e22e2.jpg', 
              price: 108, 
              saled: 850, 
              rest: true 
            },
            { name: '追源深山土蜂蜜', img: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-b1ebbd3c-ca49-405b-957b-effe60782276/3c1553fe-b9c3-476f-8598-0cd2d09d3999.jpg', price: 98, saled: 350, rest: true },
            { name: '杨梅干2罐起包邮【专拍】', img: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-b1ebbd3c-ca49-405b-957b-effe60782276/fe5343f9-b925-4759-a423-e0005f965ede.jpg', price: 69, saled: 2650, rest: true },
            { name: '追源杨梅干', img: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-b1ebbd3c-ca49-405b-957b-effe60782276/c8c4e05e-42d4-4a22-b111-85765d4e22e2.jpg', price: 69, saled: 2456, rest: true },
            { name: '追源深山土蜂蜜', img: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-b1ebbd3c-ca49-405b-957b-effe60782276/3c1553fe-b9c3-476f-8598-0cd2d09d3999.jpg', price: 258, saled: 601, rest: true },
            { name: '杨梅干2罐起包邮【专拍】', img: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-b1ebbd3c-ca49-405b-957b-effe60782276/fe5343f9-b925-4759-a423-e0005f965ede.jpg', price: 258, saled: 550, rest: true },
            { name: '追源杨梅干', img: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-b1ebbd3c-ca49-405b-957b-effe60782276/c8c4e05e-42d4-4a22-b111-85765d4e22e2.jpg', price: 12.5, before: 19, saled: 4720, rest: true },
            { name: '追源深山土蜂蜜', img: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-b1ebbd3c-ca49-405b-957b-effe60782276/3c1553fe-b9c3-476f-8598-0cd2d09d3999.jpg', price: 9.9, before: 15, saled: 4456 },
          ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      let userInfo = wx.getStorageSync('userInfo');
      if (!userInfo){
        wx.showModal({
            title:'提示',
            content:'您还没有登录',
            success(res){
                if (res.confirm){
                    // wx.switchTab({
                    //   url: '/pages/ucenter/index/index',
                    // })
                    app.getuserInfo();
                } else if (res.cancel){
                    wx.switchTab({
                        url: '/pages/index/index',
                    })
                }
            }
        })
      }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
      return {
        title:'sharetest',
        path:'/pages/index/index?shareid=' + '54185418'
      }
    },
    /**
     * (收藏页面跳转详情页)
     * @2021-2-21
     * @徐志鹏
     * @param {*} e 
     */
    onClick:function(e){
        if (!wx.getStorageSync('goodsList')){
          wx.setStorageSync('goodsList', this.data.goodsList);
        }
       
        wx.navigateTo({
          url: '/pages/product/product?index=' + e.currentTarget.dataset.index,
        })
    },
    cancel:function(e){
        let that = this;
        let index = e.currentTarget.dataset.index;
        let collectionlist = that.data.collectionList;
        
        console.log(index);
        wx.showModal({
          content: '确定移除收藏吗？',
          showCancel: true,
          title: '提示',
          success: (result) => {
              if (result.confirm){
                  collectionlist.splice(index, 0);
                  that.setData({
                      collectionList:collectionlist
                  })
                  wx.showToast({
                    title: '移除成功！',
                    icon:'success'
                  })
              } else if (result.cancel){
                  return;
              }
          },
          fail: (res) => {},
          complete: (res) => {},
        })
        collectionlist.splice(index,1);
    }
})